import { App } from 'vue'
import { useAuth } from '@/hooks/useAuth'

/**
 * 全局授权插件
 * 在 Vue 实例中注册全局的授权方法
 */
export default {
  install(app: App) {
    // 获取授权相关方法
    const auth = useAuth()
    
    // 注册全局属性
    app.config.globalProperties.$auth = auth
    
    // 注册全局方法
    app.config.globalProperties.$requireAuth = auth.requireAuth
    app.config.globalProperties.$checkAuth = auth.checkAuthWithModal
    app.config.globalProperties.$isLogined = auth.isLogined
    app.config.globalProperties.$wechatLogin = auth.wechatLogin
    app.config.globalProperties.$logout = auth.logout
  }
}

// 声明全局属性类型
declare module '@vue/runtime-core' {
  interface ComponentCustomProperties {
    $auth: ReturnType<typeof useAuth>
    $requireAuth: ReturnType<typeof useAuth>['requireAuth']
    $checkAuth: ReturnType<typeof useAuth>['checkAuthWithModal']
    $isLogined: ReturnType<typeof useAuth>['isLogined']
    $wechatLogin: ReturnType<typeof useAuth>['wechatLogin']
    $logout: ReturnType<typeof useAuth>['logout']
  }
}
